<template>
  <div class="LogManagement">
    <div class="filter">
      <el-form ref="form" label-width="90px">
        <div class="operation">
          <el-form-item label="日志类型:">
            <el-select v-model="getOperation.type" clearable placeholder="请选择">
              <el-option
                v-for="item in types"
                :key="item.value"
                :label="item.text"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间:">
            <el-date-picker
              v-model="getOperation.startTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd"
              :picker-options="startPickerOptions"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间:">
            <el-date-picker
              v-model="getOperation.endTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd"
              :picker-options="endPickerOptions"
            ></el-date-picker>
          </el-form-item>
          <el-button class="but-query" type="text" size="medium" @click="queryClick">
            <i class="query-icon"></i>查询
          </el-button>
        </div>
      </el-form>
    </div>
    <div class="content">
      <div class="content-head">日志列表</div>
      <div class="content-list">
        <el-table
          :data="tableData"
          border
          stripe
          size="mini"
          class="el-table-redefine"
          style="width: 100%;"
          height="100%"
        >
          <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
          <el-table-column label="日志类型" width="100px">
            <template slot-scope="scope">
              <span>{{scope.row ? matchArrayContentStr(scope.row.type, types) : "未知"}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="日志内容"></el-table-column>
          <el-table-column prop="createTime" label="日志时间" width="200px"></el-table-column>
          <el-table-column prop="user.account" label="创建者" width="150px"></el-table-column>
          <el-table-column prop="device.name" label="变电站" width="280px"></el-table-column>
        </el-table>
      </div>
      <div class="pagination-area">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper, slot"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[20, 50, 100, 300]"
          :page-size="getOperation.size"
          :current-page="getOperation.page"
          :total="tansksTotal"
        >
          <el-button type="text" size="mini">前往</el-button>
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { operationLogs } from "@/api/apiManagement";
import { matchArrayContent, LogsTypes } from "@/utils/constant";
import { dateToDateStr, getDateTopMonth} from "@/utils/utilsMethod";

export default {
  data() {
    return {
      robot_id: null,
      types: LogsTypes,
      startPickerOptions: {
        disabledDate: time => {
          let endTime = this.endTime === null ? "" : this.endTime;
          return (
            time.getTime() > new Date(endTime).getTime() ||
            time.getTime() > Date.now() - 8.64e6
          );
        }
      },
      endPickerOptions: {
        disabledDate: time => {
          let startTime = this.startTime === null ? "" : this.startTime;
          return (
            time.getTime() <
              new Date(startTime).getTime() - 8 * 60 * 60 * 1000 ||
            time.getTime() > Date.now() - 8.64e6
          ); //如果没有后面的-8.64e6就是不可以选择今天的   减去一天的时间代表可以选择同一天
        }
      },
      tableData: [],
      getOperation: {
        page: 0,
        size: 20,
        type: "",
        startTime: "",
        endTime: ""
      },
      tansksTotal: 0
    };
  },
  created() {
    this.robot_id = localStorage.getItem("ydRobot_id");
    this.getOperation.startTime = dateToDateStr(getDateTopMonth(new Date()));
    this.getOperation.endTime = dateToDateStr(new Date());
    // 
    this.getOperationLogs();
  },
  mounted() {},
  methods: {
    getOperationLogs() {
      if (this.getOperation.type === null) {
        this.getOperation.type = '';
      }
      this.getOperation.startTime =
        this.getOperation.startTime == null
          ? ""
          : this.getOperation.startTime;
      this.getOperation.endTime =
        this.getOperation.endTime == null ? ""
          : this.getOperation.endTime.length <= 10 && this.getOperation.endTime.length != 0
          ? this.getOperation.endTime + " 24:00:00"
          : this.getOperation.endTime;
      operationLogs(this.getOperation)
        .then(response => {
          if (response) {
            this.tableData = response.list;
            this.tansksTotal = response.total;
          }
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.getOperation.size = val;
      this.getOperationLogs();
    },
    handleCurrentChange(val) {
      this.getOperation.page = val;
      this.getOperationLogs();
    },
    // 匹配类型返回
    matchArrayContentStr(val, array) {
      return matchArrayContent(val, array);
    },
    queryClick() {
      this.getOperationLogs();
    }
  }
};
</script>

<style>
.LogManagement .filter .el-form-item {
  margin-top: 0;
  margin-bottom: 3px;
}
.LogManagement .filter .el-form-item__label {
  font-size: 13px;
  line-height: 26px;
}
.LogManagement .filter .el-form-item__content {
  font-size: 13px;
  line-height: 26px;
}
.LogManagement .filter .el-checkbox {
  font-size: 13px;
}
.LogManagement .filter .el-checkbox__label {
  font-size: 13px;
}
.LogManagement .filter .el-checkbox {
  width: 100px;
  margin-left: 10px;
}
.LogManagement .filter .el-checkbox + .el-checkbox {
  margin-left: 10px;
}
/*  */

.LogManagement {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column;
}
.LogManagement .filter {
  position: relative;
  width: 100%;
  /* height: auto; */
  height: 30px;
  background-color: #eeeeee;
}
.LogManagement .operation {
  width: 100%;
  height: 30px;
  background: linear-gradient(
    rgb(227, 242, 238),
    rgb(202, 231, 238)
  ); /* 标准的语法 */
  border-top: 1px solid rgb(202, 231, 238);
}

.LogManagement .operation .el-form-item {
  margin-top: 1px;
  margin-bottom: 0;
  float: left;
}
.LogManagement .operation .el-form-item__content {
  width: 200px;
  margin-left: 5px !important;
}
.LogManagement .operation .el-form-item__content {
  /* width: 200px; */
  margin-left: 5px !important;
  line-height: 26px;
  float: left;
}
.LogManagement .operation .el-date-editor.el-input {
  width: 200px;
}
.LogManagement .operation .el-input__inner {
  height: 26px;
  line-height: 26px;
}
.LogManagement .operation .el-input__icon {
  line-height: 26px;
}
.LogManagement .but-query,
.LogManagement .but-reset {
  height: 30px;
  margin-left: 20px;
  margin-right: 10px;
  padding: 0;
  font-size: 13px;
  color: #000;
  float: left;
}
.LogManagement .query-icon {
  width: 14px;
  height: 14px;
  margin-right: 3px;
  background-image: url(../../../static/resources/images/query.png);
  background-size: 100% 100%;
  float: left;
}
.LogManagement .reset-icon {
  width: 14px;
  height: 14px;
  margin-right: 3px;
  background-image: url(../../../static/resources/images/reset_a.png);
  background-size: 100% 100%;
  float: left;
}

.LogManagement .content {
  position: relative;
  width: 100%;
  flex-grow: 1;
}
.LogManagement .content-head {
  width: 100%;
  height: 27px;
  padding: 0 5px;
  line-height: 27px;
  font-size: 13px;
  background: linear-gradient(
    rgb(227, 242, 238),
    rgb(202, 231, 238)
  ); /* 标准的语法 */
}

.LogManagement .content-list {
  position: absolute;
  width: 100%;
  height: calc(100% - 59px);
  /* overflow: auto; */
}

.LogManagement .el-table-redefine {
  width: 100%;
  height: 100%;
  /* display: flex;
  flex-flow: column; */
}

.LogManagement .el-table-redefine .el-table__body-wrapper {
  /* overflow: hidden;
  overflow-y: auto;
  flex-grow: 1; */
}

.LogManagement .pagination-area {
  position: absolute;
  width: 100%;
  height: 32px;
  bottom: 0px;
  background-color: #eeeeee;
}
</style>